package org.lazycoder.logger.module.ask3;

import java.util.Observable;
import java.util.Observer;
import java.util.Scanner;

import org.lazycoder.logger.Event;

/**
 * Observes log files.
 * 
 * @author Matthew Bayliss
 * 
 */
public class LogObserver implements Observer {
	private static final String ACTION_CASE_CLOSE = ".*org.sleuthkit.autopsy.casemodule.CaseCloseAction$";
	private static final String ACTION_OPEN_CASE = ".*Opening case.configFilePath:.*";
	private static final String ACTION_ADD_IMAGE = ".*addImageINFO: Adding image to Case.*";
	private static final String ACTION_NEW_CASE = ".*createINFO: Creating new case.*";
	private static final String REGEX_URL = "[A-Z]:\\\\.*?\\.[a-zA-Z0-9]{3}";
	private static final String REGEX_NEW_CASE_NAME = "[A-Z]:\\\\.*?$";
	private final ASK3 owner;

	/**
	 * @param owner
	 *            use "this" keyword.
	 */
	public LogObserver(ASK3 owner) {
		this.owner = owner;
	}

	@Override
	public void update(Observable arg0, Object arg1) {
		String message = arg1.toString();
		Event event = null;
		if (message.matches(ACTION_CASE_CLOSE)) {
			event = new Event("Case closed");
		} else if (message.matches(ACTION_OPEN_CASE)) {
			String location = "";
			Scanner scanner = new Scanner(message);
			location = scanner.findInLine(REGEX_URL);
			scanner.close();
			event = new Event("Case opened " + location);
		} else if (message.matches(ACTION_ADD_IMAGE)) {
			String location = "";
			Scanner scanner = new Scanner(message);
			location = scanner.findInLine(REGEX_URL);
			scanner.close();
			event = new Event("Adding image to case: " + location);
		} else if (message.matches(ACTION_NEW_CASE)) {
			String location = "";
			Scanner scanner = new Scanner(message);
			location = scanner.findInLine(REGEX_NEW_CASE_NAME);
			scanner.close();
			event = new Event("New Case: " + location);
		} else {
			System.out.println("Unhandled log message: " + message);
		}
		owner.update(event);
	}

}
